﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TS.DataAccess;
using TS.Entities;

namespace TS.Bussiness
{
    public class ScheduleBussiness
    {
        public int InsertSchedule(Schedule schedule)
        {
            var scheduleDao = new ScheduleDataAccess();
            try
            {
                return scheduleDao.InsertSchedule(schedule);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }


        /// <summary>
        /// Select Schedule pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="day"> </param>
        /// <param name="ownerId"></param>
        /// <param name="totalRecord"></param>
        /// <param name="year"> </param>
        /// <param name="month"> </param>
        /// <param name="week"> </param>
        /// <returns></returns>
        public Collection<Schedule> ReadSchedulePaged(int pageSize, int pageIndex, string strFilter
                                                            , string strOrderBy
                                                            , string year, string month, string week, string day,
                                                            string ownerId,string status,
                                                            out int totalRecord)
        {
            totalRecord = 0;
            var dataAccess = new ScheduleDataAccess();
            try
            {
                if (pageIndex >= 0 && pageSize > 0)
                {
                    var result = dataAccess.ReadSchedulePaged(pageSize, pageIndex, strFilter, strOrderBy,
                                                                year,month,week,day,
                                                                 ownerId, status
                                                                ,out totalRecord);
                    if (result != null)
                    {
                        return result;
                    }
                    return null;
                }
                return null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }


        /// <summary>
        /// Get All Schedule
        /// </summary>
        /// <returns></returns>
        public Collection<Schedule> GetAllSchedule()
        {
            ScheduleDataAccess grpDao = new ScheduleDataAccess();
            try
            {
                Collection<Schedule> grlList = new Collection<Schedule>();
                grlList = grpDao.GetAllSchedule();
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        public Schedule GetScheduleById(int ScheduleId)
        {
            var grpDao = new ScheduleDataAccess();
            try
            {
                var grlList = new Schedule();
                grlList = grpDao.GetByScheduleId(ScheduleId);
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        public int UpdateSchedule(Schedule schedule)
        {
            var scheduleDao = new ScheduleDataAccess();
            try
            {
                return scheduleDao.UpdateSchedule(schedule);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public int DeleteSchedule(Schedule schedule)
        {
            if (schedule.ScheduleId <= 0) return -1;
            var scheduleDao = new ScheduleDataAccess();
            try
            {
                return scheduleDao.DeleteSchedule(schedule);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }
    }
}
